<template>
  <div id="presonalFrom">
    <p>{{title}}日记</p>
    <el-form ref="form" :model="form" label-width="160px">
      <!-- 标题 -->
      <el-form-item label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <!-- 上传缩略图 -->
      <el-form-item label="缩略图">
        <el-upload
          action="#"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="handleFileChange"
          :file-list="fileList"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}">
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-form-item>
      <!-- 内容区 -->
      <el-form-item label="内容">
        <div id="editor" ref="editorElm"></div>
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即发布</el-button>
        <el-button @click="$router.push('/personal')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import E from "wangeditor";
export default {
  name: "PresonalFrom",
  props: {
    form: {
      type: Object,
      default: () => {
        return {};
      }
    },
    fileList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      // 默认不显示图片预览
      dialogVisible: false,
      disabled: false,
      //图片预览地址
      dialogImageUrl: "",
      title: "发表"
    };
  },
  methods: {
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleFileChange(file) {
      this.form.description_image_path = file.raw;
    },
    // 富文本设置
    handleOpen() {
      // 将原先的富文本编辑器清空，防止嵌套 BUG
      this.$refs.editorElm.innerHTML = "";
      // 实例化富文本编辑器
      this.editor = new E("#editor");
      // 当富文本器中的内容发生改变时，把内容赋值到表单数据中
      this.editor.customConfig.onchange = html => {
        this.form.content = html;
      };
      // this.editor.customConfig.menus = ["head", "bold", "italic", "underline"];
      this.editor.create();
      this.editor.txt.html(this.form.content);
    },
    // 编辑和添加
    onSubmit() {
      let { id, realname } = JSON.parse(sessionStorage.getItem("user"));
      this.form.time = Date.now();
      this.form.user_id = id;
      this.form.realname = realname;
      let form = new FormData();
      for (let i in this.form) {
        form.append(i, this.form[i]);
      }
      this.$emit("handleRequest", form);
    }
  },
  mounted() {
    // 在父组件无传递数据时直接挂载
    this.handleOpen();
  },
  watch: {
    // 监听父组件传递过来的数据
    form(newVal) {
      // 父组件传递数据后二次挂载
      this.handleOpen();
      this.title = '编辑'
    }
  }
};
</script>
<style lang='less'>
#presonalFrom {
  padding: 20px 100px 0 0;
  .el-form-item__label {
    font-size: 16px;
  }
  p{
      font-size: 26px;
      text-align: center;
  }
}

// 图片上传样式
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>